package com.china.great.system.controller;

import com.china.great.system.common.CommonResultUtil;
import com.china.great.system.entity.Blog;
import com.china.great.system.service.BlogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Liu HongYang
 * @DateTime: 2020/5/8 9:42 下午
 * @Description: TODO
 */
@RestController
@Api("博客相关的api")
@RequestMapping("/api/v1")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping(value = "/getBlogList", method = RequestMethod.GET)
    @ApiOperation(value="获取列表")
    public Object getList(@RequestParam(value="pageSize", defaultValue="20") int pageSize,
                          @RequestParam(value="pageNum", defaultValue="1") int pageNum){
        PageInfo list = blogService.getList(pageSize,pageNum);
        return CommonResultUtil.success(list);
    }

    @RequestMapping(value = "/insertBlog", method = RequestMethod.POST)
    @ApiOperation(value="增加博客")
    public Object insert(@RequestBody Blog blog){
        int i = blogService.insert(blog);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

    @RequestMapping(value = "/updateBlog", method = RequestMethod.POST)
    @ApiOperation(value="修改博客")
    public Object update(@RequestBody Blog blog){
        int i = blogService.update(blog);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

    @RequestMapping(value = "/deleteBlog", method = RequestMethod.GET)
    @ApiOperation(value="删除博客")
    public Object delete(@RequestParam int id){
        int i = blogService.delete(id);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

}
